package com.power.using.sharlockchat.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.igexin.sdk.PushManager;
import com.power.using.sharlockchat.R;
import com.power.using.sharlockchat.base.BaseActivity;
import com.power.using.sharlockchat.fragment.MainFragment;
import com.power.using.sharlockchat.interfaces.DialogClick;
import com.power.using.sharlockchat.service.DownLoadService;
import com.sunfusheng.daemon.DaemonHolder;

import java.io.File;

public class MainActivity extends BaseActivity {

    private MyReceiver mReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       // StatusBarUtil.setColor(this, ContextCompat.getColor(this, R.color.blue_300),0);
        if (findFragment(MainFragment.class) == null) {
            loadRootFragment(R.id.fl_container, MainFragment.newInstance());
        }

        String clientid = PushManager.getInstance().getClientid(this);
        Log.i("getclientid", "onCreate: "+clientid);





    }

    @Override
    protected void initContentView(Bundle savedInstanceState) {

    }

    @Override
    protected void initView() {

        mReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.sibosi.yzdsupervisep.service.DownLoadService");
        this.registerReceiver(mReceiver, filter);

    }

    @Override
    protected void initLogic() {
       // startService(new Intent(this, DownLoadService.class));

    }

    public class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(final Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            final String filepath = bundle.getString("file");
            Log.e("filepath", filepath + intent.getAction());
            if (intent.getAction().equals("com.sibosi.yzdsupervisep.service.DownLoadService")) {

                showAlertDialog2(context, "更新", "更新了，赶快去下载吧", new DialogClick() {
                    @Override
                    public void PositiveMessage() {
                        if (!filepath.equals("")) {

                            File file = new File(filepath);
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            Uri uri = Uri.fromFile(file);
                            String type = "application/vnd.android.package-archive";
                            intent.setDataAndType(uri, type);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);

                        }
                    }

                    @Override
                    public void NegativeMessage() {
                        stopService(new Intent(context, DownLoadService.class));
                    }
                });
                //  mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

            }

        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        DaemonHolder.startService();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DaemonHolder.stopService();
        unregisterReceiver(mReceiver);
    }
}
